import 'dart:async';
import 'dart:io';

import 'package:ali_vod_video/upload_callback.dart';
import 'package:flutter/services.dart';

class AliVodVideo {
  MethodChannel? _channel;


  AliVodVideo(){
    _channel = MethodChannel('ali_vod_video');
  }

  Future<String> get platformVersion async {
    final String version = await _channel?.invokeMethod('getPlatformVersion');
    return version;
  }

  void registerNativeListener(OnNativeMethodListener listener){
    _channel?.setMethodCallHandler((MethodCall call) async {
      switch (call.method) {
        case 'onUploadProgressChange':
        ///上传附件进度监听
          double progress = call.arguments;
          listener.onUploadProgress(progress);
          print("单独上传进度：$progress");
          break;
      }
    });
  }

  ///取消上传视频
  void cancelUploadVideoAll(){
    _channel?.invokeMethod('cancelUploadVideoAll');
  }

  ///取消上传视频
  void cancelUploadVideo(String id, String path){
    _channel?.invokeMethod('cancelUploadVideo', <String, dynamic>{
      'id': id,
      'filePath': path,
    });
  }

  ///上传视频
  void uploadVideo(String id, String cateId, String templateGroupId, String definition,
      String path, String userData, UploadCallback uploadCallback, {bool? needProgress,}) async {
    if (path == null || path.isEmpty) {
      uploadCallback.onError("文件地址为空");
      return;
    }
    File video = File(path);
    bool exists = await video.exists();
    if (!exists) {
      uploadCallback.onError("文件不存在");
      return;
    }
    try {
      _channel?.setMethodCallHandler((MethodCall call) async {
        switch (call.method) {
          case 'onUploadProgressChange':
          ///上传附件进度监听
            String tempPath = call.arguments["filePath"];
            String id = call.arguments["id"];
            double progress = call.arguments["progress"];
            uploadCallback.onProgress(progress, id, msg:tempPath);
            // print("上传进度：$progress ------------------$tempPath");
            break;
          case 'onUploadCancel':
          ///上传取消监听
            String tempPath = call.arguments["filePath"];
            String id = call.arguments["id"];
            uploadCallback?.onCancel(id, tempPath);
            break;
        }
      });
      Map result = await _channel?.invokeMethod('uploadVideo', <String, dynamic>{
        'filePath': path,
        'id': id,
        'cateId': cateId,
        'templateGroupId': templateGroupId,
        'definition': definition,
        'UserData': userData,
        'needProgress': needProgress??false,
      });
      return uploadCallback.onSuccess(result["url"],result["fileSize"],
          result["coverUrl"],result["videoId"],
          result["sdVideo"], result["sdStorage"]
      );
    } on PlatformException catch (e) {
      return uploadCallback.onError(e.message??'');
    } on Exception catch (e) {
      return uploadCallback.onError(e.toString());
    }
  }
}

///上传进度变更
typedef OnUploadProgress = void Function(double progress);
///原生信息
class OnNativeMethodListener {
  ///上传进度
  OnUploadProgress onUploadProgress;
  OnNativeMethodListener(this.onUploadProgress);
}

